home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / VGOPHONG.ZIP / VGOPHONG.DOC < prev   
Encoding:
Text File  |  1997-02-17  |  17.9 KB  |  466 lines

  1.  
  2. ─────────────────────────────────────────────────────────────────────────────
  3.  
  4.   VGOPHONG.TXT - Phong lighting and specular highlights. Theory,
  5.          practice and explaination of the phong lighting
  6.          and shading model.
  7.  
  8. ─────────────────────────────────────────────────────────────────────────────
  9.  
  10.   by TimJ/Vertigo
  11.  
  12.   "I am he, as you are he, as you are me, and we are all together"
  13.  
  14.   email: tim@legend.co.uk
  15.   irc:   #coders #vertcode
  16.  
  17.  
  18.   revision history:
  19.  
  20.   16/02/97      v1.0    -       Initial version
  21.  
  22.  
  23. ─────────────────────────────────────────────────────────────────────────────
  24.  INTRODUCTION
  25. ─────────────────────────────────────────────────────────────────────────────
  26.  
  27.   First off, I hope this doc is of use to some people, and maybe other
  28.   people will find it interesting.
  29.  
  30.   Recently, I've been thinking a lot about phong shading and lighting.
  31.   There was something that was bugging me. I couldn't quite put my finger
  32.   on it. It was something I knew to be true, but I had to explain it to
  33.   myself. It all started when I was chatting to Vector about true phong
  34.   shading. We'd both recently looked at Voltaire/OTM's doc on fast phong
  35.   shading (again).
  36.  
  37.   We were (among other things) chatting about lighting functions in our
  38.   3D engine. It was about specular highlights and the way normal (fast)
  39.   phong lighting doesn't yield specular highlights on a flat plane
  40.   (all normals pointing one way). This got me thinking. I thought, well
  41.   of course it doesn't, because the light calculated at each point will be
  42.   the same (because all normals are the same). This annoyed me because I
  43.   knew it wasn't true -- but I couldn't remember why. It's all to do with
  44.   the light vector and the view vector (and keeping them constant).
  45.  
  46.   Volatire's phong method also doesn't yield specular highlights in the
  47.   center of polygons. Oh, and his method emulates the equation given
  48.   exactly -- it's not a tradeoff. But then again, it's also just the
  49.   same as using gouraud. I'll explain later.
  50.  
  51.   Then I remembered the actual theory behind the lighting equations. It
  52.   stuck me that people tend to get mixed up in code and forget about the
  53.   theory behind it.
  54.  
  55.   What I'll do is go through the theory, how it's implemented.
  56.   Based on that I'll then address what this doc is actually about --
  57.   highlights in the center of polygons and on flat planes.
  58.  
  59.   If you think this doc is a bit slow, forgive me, but you can never please
  60.   everybody :)
  61.  
  62.   [Oh, important point.. this is generally about the phong equation. You
  63.    can do it per pixel or per vertex, either way it's the phong equation.
  64.    Just because you gouraud shade doesn't mean you can't have phong style
  65.    specular highlights.]
  66.  
  67.  
  68. ─────────────────────────────────────────────────────────────────────────────
  69.  LIGHT RAY REFLECTION
  70. ─────────────────────────────────────────────────────────────────────────────
  71.  
  72.   I was going to explain the theory behind light ray relfection and the
  73.   spread of the ray across a surface depending on the angle of incidence.
  74.   But but then I realized I'd have to go into they physics behind spectral
  75.   reflectivity too, so I won't :)
  76.  
  77.   If there's enough demand for it, email me and I'll put it in.
  78.  
  79.  
  80. ─────────────────────────────────────────────────────────────────────────────
  81.  PHONG'S SPECULAR HIGHLIGHTS
  82. ─────────────────────────────────────────────────────────────────────────────
  83.  
  84.   We need to understand how the phong lighting equation is made up. Let's
  85.   define a few useful values:
  86.  
  87.  
  88.           ^N
  89.           |
  90.         L     |     R        V
  91.          \    |    /      __/
  92.           \   |   /    __/
  93.            \  |  /  __/
  94.         \ | /__/
  95.          \|//
  96.      -------------.--------------
  97.           P
  98.           ^
  99.           point under consideration
  100.  
  101.  
  102.   It's important you know what these values actually are:
  103.  
  104.   N    = surface normal
  105.   L     = unit vector between point and light
  106.   V    = unit vector between point and view
  107.   R     = light reflection unit vector (mirror of L about N)
  108.  
  109.  
  110.   First, the diffuse relfection is given by the Lamertian Relfection
  111.   equation:
  112.  
  113.     diffuse = Kd * (N dot L)
  114.  
  115.   Where Kd is the diffuse relfection constant. (N dot L) is the same as
  116.   the cosine of the angle between N and L, so as the angle decrease, the
  117.   resulting diffuse value is higher.
  118.  
  119.   Phong gave spectral reflectivity as:
  120.  
  121.     diffuse + Ks * (R dot V)^n
  122.  
  123.   Which is:
  124.  
  125.     Kd * (N dot L) + Ks * (R dot V)^n
  126.  
  127.   Where Kd is the diffuse component and Ks is the specular compoenet. This
  128.   is the generally accepted phong lighting equation. Ks is generaly taken to
  129.   be a specularity constant (although Phong defined it as W(i).. see later).
  130.  
  131.   As the angle between the view (V) and the reflected light (R) decreases,
  132.   you will get more specularity.
  133.  
  134.   The clever thing about Phong's equation was that it gave a neat way to
  135.   calculate the specular intensity 'bump' around the light reflection
  136.   vector (R). The larger the exponential power (n) the smaller and more
  137.   intense the specular intensity bump. Hence specular highlights.
  138.  
  139.  
  140. ─────────────────────────────────────────────────────────────────────────────
  141.  IMPLEMENTATION OF PHONG'S EQUATION
  142. ─────────────────────────────────────────────────────────────────────────────
  143.  
  144.   Most people simplify this equation somewhat, for speed. We begin with :
  145.  
  146.     Kd * (N dot L) + Ks * (R dot V)^n
  147.  
  148.   The obvious thing we'd like to remove is (R dot V). Since we don't
  149.   want to calculate the light relfection vector (mirror of light incidence
  150.   around the surface normal) -- because it's expensive. Blinn introduced a
  151.   way to do this using an imaginary vector H. It's then reduced to (N dot H).
  152.   H is defined as halfway between L and V (after L and V are normalized).
  153.  
  154.   H is therefore (L + V) / 2. You will see that the angle R dot V is double
  155.   N dot H -- but this doesn't matter as you can alter the specular
  156.   exponential value (n) to compenstate. This gives us the equation :
  157.  
  158.     Kd * (N dot L) + Ks * (N dot ( L + V / 2))^n
  159.  
  160.   Up until now we've ignored the ambient factor, this is because it's
  161.   damn obvious and has little consequence on the math.. we'll put it in
  162.   now
  163.  
  164.     Ka + Kd * (N dot L) + Ks * (N dot ( L + V / 2))^n
  165.  
  166.   Which is easily implemented. You only need three vectors: the surface
  167.   normal, the light vector and the view vector. It's obviously advised
  168.   to do this equation in object-space.
  169.  
  170.  
  171.   Another way to remove R dot V, is by replacing it with N dot L :
  172.  
  173.     Ka + Kd * (N dot L) + Ks * (N dot L)^n
  174.  
  175.   This assumes you will always get the maximum specularly reflected light,
  176.   no matter where the view is. Here's why :
  177.  
  178.   If we assume V is always the same as R, then the angle between N and V is
  179.   the same as N and L --
  180.  
  181.           ^N
  182.           |                     A = angle between N and L
  183.         L     |     R (also V)      B = angle between N and V
  184.          \  A | B  /
  185.           \  /|\  /
  186.            \/ | \/
  187.         \ | /
  188.          \|/
  189.      -------------.--------------
  190.  
  191.  
  192.   Angle A and B are the same (of course, since R is the mirror vector of L).
  193.   So, N dot V becomes the same as L dot N.
  194.  
  195.   This makes life easier and faster. The results completely ignore the
  196.   position of the view; so it's like having a reflective surface that always
  197.   reflects the maximum amount of specular light towards the view.
  198.  
  199.     (normally, as the angle between the view and the reflected light
  200.      increases, you get less specularly reflected light).
  201.  
  202.   It's just a trade off.
  203.  
  204.     Ka + Kd * (N dot L) + Ks * (N dot L)^n
  205.  
  206.   or
  207.  
  208.     Ka + Kd * cos(theta) + Ks * cos(theta)^n
  209.  
  210.   where cos(theta) is N dot L. Most likely, the above equation is the
  211.   one most people use. Also, since more implementation assume V is constant
  212.   across the scene (normalized.. at infinity) then using N dot L can be
  213.   acceptable. But it does have some dire consequences.
  214.  
  215.  
  216. ─────────────────────────────────────────────────────────────────────────────
  217.  REAL TIME PHONG SHADING
  218. ─────────────────────────────────────────────────────────────────────────────
  219.  
  220.   This is what was causing the confusion. Voltaires text on phong shading
  221.   (OTMPHONG.TXT) used the equation
  222.  
  223.     color = specular + (cos x) * diffuse + (cos x)^n * specular
  224.  
  225.   for calculating phong lighting. This is the same as the last equation we
  226.   just disussed. He then went on to explain the specular intensity 'bump'
  227.   through the (cos x)^n term of the equation.
  228.  
  229.   (note: phong shading is done by recalculating the lighting equation at
  230.    each pixel -- this is done by interpolating the vertex normals across
  231.    the polygon and re-evaluating).
  232.  
  233.   Since there is just one angle term in the equation (N dot L), he realized
  234.   he could dispense with normals and just interpolate the angle.
  235.  
  236.   Remember :
  237.  
  238.     Ka + Kd * cos(theta) + Ks * cos(theta)^n
  239.  
  240.   You only need to interpolate theta, then you can do a lookup table for
  241.   the correct colour created like so:
  242.  
  243.   for( theta=0 ; theta < 90 ; theta++ )
  244.   {
  245.     table[i] = Ka + Kd * cos(theta) + Ks * pow( cos(theta) , specExp ) ;
  246.   }
  247.  
  248.   The problem with all this is that the original equation was inaccurate,
  249.   so the results will be inaccurate. However, Voltaire does point this
  250.   out, and state that highlights can't be inside polygons.
  251.  
  252.   But, as Zog pointed out to me, you can get exactly the same effect with
  253.   gouraud, by setting up the palette in a similar way. This method is
  254.   basically the same as gouraud, you're just interpolating an angle instead
  255.   of an intensity.. as Zog put it, "it's fucking gouraud revisited" :)
  256.  
  257.   I just thought I'd clear this up, as people tend not to believe it's
  258.   real, and think it's some kind of trick (if you use the same equation
  259.   in a true shader, you'll get the same results).
  260.   It's also important for the next section (the equation at least).
  261.  
  262. ─────────────────────────────────────────────────────────────────────────────
  263.  PHONG'S SPECULAR HIGHLIGHTS (REVISITED)
  264. ─────────────────────────────────────────────────────────────────────────────
  265.  
  266.   As pointed out, phong shading requires the interpolating of vertex
  267.   normals across the polygon, and recalculation of the equation.
  268.  
  269.   Right, here's where more confustion comes in. To simplify things,
  270.   people tend to treat V as a constant over the entire scene.
  271.  
  272.                  N
  273.              L        |    / V (view)
  274.            (light) \      |   /
  275.              \    |  /
  276.                \  | /
  277.                  \|/
  278.          -------------.--------------
  279.                   P
  280.  
  281.  
  282.  
  283.   V is not constant though.. it is dependant on the point under
  284.   consideration (P). So making V constant, is like sticking the view at
  285.   infinity (this is done by normalizing the view vector).
  286.   This means that the falloff of the specular light at sharp angles
  287.   between the surface and view is not taken into account (it's linear).
  288.   So the highlight will be too big and intense at sharp angles (the
  289.   falloff will be linear in respect to the view position). Also, the
  290.   highlight won't move correctly with the view.
  291.  
  292.   You can probably see that the same thing can be done for L..
  293.   ie. directional lighting. Putting L at infinity affects specular fall
  294.   off with respect to the light and the view. But I'm sure everyone knows
  295.   the implications of directional lighting (it's just like having a light
  296.   really really far away).
  297.  
  298.  
  299.   As explained earlier, dispensing with V altogether can give you a nice
  300.   speed up using N dot L instead of R dot V.
  301.  
  302.   Let's look at the consequences of dispensing with V. This is like
  303.   assuming you have a perfect reflector.. like a mirror. The surface will
  304.   always reflect the maximum amount of specular light towards the view --
  305.   the highlight will seem to 'stick' to the light relfection vector and not
  306.   change shape or size, no matter where you put the view.
  307.  
  308.   But, given this, you just have to interpolate N for the polygon
  309.  
  310.     (remember, we need (N dot L) and (N dot H) where is H = L+V/2)
  311.  
  312.  
  313. ─────────────────────────────────────────────────────────────────────────────
  314.  SPECULAR REFLECTION
  315. ─────────────────────────────────────────────────────────────────────────────
  316.  
  317.   Now we get to the crux of the problem. All that time ago back at the
  318.   start, I mentioned a plane with all the normals pointing one way.
  319.  
  320.   The thing to remember is this, the lighting is *not* just dependant on
  321.   the surface normal. It's a function of the light vector and the view
  322.   vector. The important value is V, as V affects how specular light is
  323.   reflected.
  324.  
  325.   If V is properly calculated, for a flat surface, the angle between the
  326.   view and the normal (which is constant) will alter.
  327.  
  328.                   N
  329.     V                    ^
  330.     \ \                  |
  331.      \   \               |
  332.       \     \            |
  333.        \       \         |
  334.         \         \      |
  335.          \           \   |
  336.        -------.-----------.---
  337.           p1          p2
  338.  
  339.  
  340.   The angle at p1 is obviously sharper than the angle at p2. Even though
  341.   N is the same at both points. It all becomes obvious now. Unless you
  342.   calculate V correctly, the reflected specular light over a flat surface
  343.   will be the even at any point on it.
  344.  
  345.   At this point you might be thinking : "what if V is put at infinity and
  346.   L is calculated properly? -- won't that do the same job?"
  347.  
  348.   In a word, no, because V is the important vector. We have to remember the
  349.   original equation where the specular light was a function of R and V.
  350.   N would be constant, V would be constant, so specular light would just be
  351.   a function of V -- ie, not very accurate at all.
  352.  
  353.   So, we're left with the following equation:
  354.  
  355.     Ka + Kd * (N dot L) + Ks * (N dot ( L + V / 2))^n
  356.  
  357.   So, basically, in a nutshell, you've got to recalculate V for the new
  358.   point under consideration. If you do that, you'll get specular highlights
  359.   on flat planes.
  360.  
  361.   If you have directional lights (L at infinity) then the highlight will be
  362.   incorrectly positioned and spread -- since the angle between any given
  363.   point and the light will be more or less the same (to an infintecial
  364.   degree). On a flat surface you'd be depending on the angle the view makes
  365.   with the surface. Although, this isn't too much to worry about --
  366.   I think directional lighting is a choice, not a compromise.
  367.  
  368.     [ Oh yeah, in the first section I said Phong defined Ks as W(i). Well,
  369.       this meant that Ks was a function of the angle of incidence between
  370.       the light and the surface. So specularly reflected light was dependant
  371.       on the incoming angle as well as the outgoing angle. Phong never
  372.       actually defined W(i) though -- so it's usually ignored. It does give
  373.       you another parameter for your surface to play with though. ]
  374.  
  375. ─────────────────────────────────────────────────────────────────────────────
  376.  REAL TIME PHONG SHADING (REVISIED)
  377. ─────────────────────────────────────────────────────────────────────────────
  378.  
  379.   Ok, so now we know that V and L are important factors of the equation.
  380.   Voltaire's phong shading method is completely correct for the equation
  381.   he used (but as mentioned, it's basically the same as gouraud).
  382.  
  383.   What he did was place L at infinity and make the surface a perfect
  384.   reflector (that always reflected the maximum specular light) towards
  385.   the view (where ever it was). I explained all this earlier anyway :)
  386.  
  387.   That meant there was only one angle to interpolate, but it also meant
  388.   that on flat surfaces it was impossible to get correct highlights.
  389.   (polygons are flat :)
  390.  
  391.   However, Voltaire's method is more accurate than normal gouraud for the
  392.   same lighting equation (due to the non-linear lookup table).
  393.   However, you can do the same with gouraud.. it just another way of
  394.   implenenting it (and quite redundant).
  395.   His method won't extend to other equations though.
  396.  
  397.   What's annoying is when people just say his method doesn't work and is
  398.   a load of crap, without explaining why.
  399.  
  400.  
  401. ─────────────────────────────────────────────────────────────────────────────
  402.  CLOSING WORDS
  403.  
  404.  "picture yourself in boat on river with tangerine trees and marmalade skies"
  405.  
  406. ─────────────────────────────────────────────────────────────────────────────
  407.  
  408.   Well, I guess I've managed to confuse almost eveyone. What I've tried
  409.   to do is explain the factors of the phong lighting equation -- what parts
  410.   do what, and why it works.
  411.  
  412.   I've probably made loads of typo's and got different bit's mixed up --
  413.   but hell, I don't care :)  With any luck, I might not get flamed.
  414.   If I have mixed something up, forgive me -- it get's hard to track what
  415.   you have/have not said etc.. and it's not easy without some good
  416.   diagrams :)
  417.  
  418.   There are a couple of sections I've left out.. I started the one on
  419.   light ray reflection, but left it out. I also has a section on optimizing
  420.   the equation :
  421.  
  422.     Ka + Kd * (N dot L) + Ks * (N dot ( L + V / 2))^n
  423.  
  424.   But I didn't get it finished (it's based on the routines I use in my
  425.   3D engine). There are no square roots, tables, divides or pow()'s used.
  426.   Yet it still produces the same results (to a reasonable degree of
  427.   accuracy -- any errors are covered up by (a). the number of intensities we
  428.   can actually display and (b). the precision of the fpu).
  429.   If there's enough demand then I'll put it in.
  430.  
  431.   At the end of all this, an interesting thing to note is that all these
  432.   equations have no physical basic what so ever -- they're just equations
  433.   that fit real work observations. Light is actually better represented as
  434.   radiation -- but let's not get into that now :)
  435.  
  436.   Greets:
  437.     Oh god, I don't know.. um.. people I know.. hmmm
  438.     (no particular order)
  439.  
  440.     Vector
  441.     Vastator
  442.     Phred
  443.     Gooroo
  444.     Midnight
  445.     aM
  446.     Eckart
  447.     codex
  448.     PGM (where ever he may be)
  449.     BigCheese
  450.     Pel
  451.     Wog (Zog, whatever)
  452.     Crom
  453.     God
  454.     All at Abstract Entertainment
  455.  
  456.     [plus anyone I've missed]
  457.  
  458.     Flames/comments go to: tim@legend.co.uk
  459.  
  460.  
  461. ─────────────────────────────────────────────────────────────────────────────
  462.  
  463.   "Living is easy with eyes closed. Misunderstanding all you see.
  464.    It's getting hard to be someone, but it all works out. It doesn't matter
  465.    much to me."
  466.